package com.example.dishit.controller.recommendation;

import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.os.Process;
import android.widget.Toast;

import com.example.dishit.controller.main.MainController;
import com.example.dishit.model.dbproxy.DBProxyFactory;
import com.example.dishit.model.dbproxy.IDBProxyCallback;
import com.example.dishit.model.dish.DishData;
import com.example.dishit.service.recommendation.IRecommendationListener;
import com.example.dishit.view.alert.AlertView;

public class RecommendationListener implements IRecommendationListener {
    private final String uniqueId;
    private final Handler handler;
    private static final String RESULT_MESSAGE_KEY = "resultMessage";
    
    public RecommendationListener(final Context context, final String uniqueId) {
        this.uniqueId = uniqueId;
        
        // Setup the HandlerThread (to draw the alert).
        final HandlerThread thread = new HandlerThread("ServiceStartArguments",
                                                       Process.THREAD_PRIORITY_BACKGROUND);
        thread.start();
        this.handler = new Handler(thread.getLooper()) {
            @Override
            public void handleMessage(Message msg) {
                final Bundle bundle = msg.getData();
                final String result = bundle.getString(RESULT_MESSAGE_KEY);
                System.out.println("RecommendationListener: handleMessage(), result = " + result);
                
                // Draw the alert.
                final DishData dish = (DishData)bundle.getSerializable(DishData.DISH_DATA_KEY);
                if(dish != null) {
                    AlertView.drawAlert(context,
                            "New Recommendation!    " + dish.getRestaurantName(),
                            dish);
                    // Automatically add the received dish to the main list.
                    MainController.instance().addReceivedDish(dish);
                } else {
                    // Otherwise, print a Toast that it failed.
                    Toast.makeText(context, "Recommendation Service failed to retrieve a restaurant.", Toast.LENGTH_LONG).show();
                }
            }
        };
    }
    
    public String getUniqueId() { return uniqueId; }
    
    public void onRecommendation(final DishData restaurant) {
        // Store-off this Dish for future use.
        DBProxyFactory.instance().storeReceivedDish(MainController.THIS_USER_ID, restaurant, new IDBProxyCallback() {
            public void onDishersCallback(List<String> myDishers) {}
            public void onDishDataCallback(List<DishData> dishDatas) {}
            public void onResultCallback(boolean wasSuccessful, String message) {
                // Handle the Dish appropriately.
                final Message msg = handler.obtainMessage();
                final Bundle bundle = msg.getData();
                
                bundle.putSerializable(DishData.DISH_DATA_KEY, restaurant);
                bundle.putString(RESULT_MESSAGE_KEY, message);
                msg.setData(bundle);
                handler.sendMessage(msg);
            }
        });
    }
}